package ustc.xiaoyang.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import ustc.xiaoyang.pojo.Result;
import ustc.xiaoyang.utils.AliyunOSSOperator;

@RestController
@Slf4j
@RequestMapping("/upload")
public class UploadController {
    private final AliyunOSSOperator aliyunOSSOperator;
    public UploadController(AliyunOSSOperator aliyunOSSOperator) {
        this.aliyunOSSOperator = aliyunOSSOperator;
    }
    @PostMapping
    public Result upload(MultipartFile file) throws Exception {
//        log.info("上传文件:{}",file.getOriginalFilename());
        if (!file.isEmpty()){
            String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
            double fileSizeMB = (double) file.getSize() / 1024 / 1024;
            String formattedSize = String.format("%.2f", fileSizeMB);
            log.info("文件上传OSS成功，url：{},大小：{}MB", url, formattedSize);
            return Result.success(url);
        }
        return Result.error("文件为空，上传失败");

    }

}
